package com.twang.stock.controller.common;

import com.google.common.collect.Lists;
import com.twang.stock.dto.BaseRequest;
import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.entity.DfcfBanKuaiMaoLiLv;
import com.twang.stock.entity.ThsHotStock;
import com.twang.stock.entity.ThsReDianStock;
import com.twang.stock.mapper.ThsReDianStockMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.ThsReDianStockService;
import com.twang.stock.service.impl.ThsReDianStockServiceImpl;
import com.twang.stock.service.jinyan.CompanyCodeInfoService;
import com.twang.stock.task.QueryBanKuaiAndDaPan;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/ths")
@Slf4j
public class ThsReDianController {

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private ThsReDianStockMapper thsReDianStockMapper;

    @Autowired
    private CompanyCodeInfoService companyCodeInfoService;

    @Autowired
    private ThsReDianStockService thsReDianStockService;

    @RequestMapping(value = "/reDian")
    public ModelAndView bigData(BaseRequest request) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<ThsReDianStock> list = thsReDianStockMapper.select24HourData();
        List<ThsHotStock> thsHotStockList=Lists.newArrayList();
        thsReDianStockService.getThsHotStockList(list,thsHotStockList);
        modelAndView.addObject("thsHotStockList",thsHotStockList);

        List<ThsReDianStock> listOne = thsReDianStockMapper.selectOneHourData();
        List<ThsHotStock> onethsHotStockList=Lists.newArrayList();
        thsReDianStockService.getThsHotStockList(listOne,onethsHotStockList);
        modelAndView.addObject("onethsHotStockList",onethsHotStockList);
        modelAndView.setViewName("ths_re_dian");
        return modelAndView;
    }


    /**
     *
     */
    @RequestMapping(value = "/stockKanDian")
    public ModelAndView stockKanDian(String code,String type) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        if(StringUtils.isEmpty(code)){
            throw new Exception("字段不能为空！");
        }
        code=code.replace("redian_","").replace("sh","").replace("sz","").trim();
        if(StringUtils.isEmpty(type)){
            type=QueryBanKuaiAndDaPan.oneHourTag;
        }
        CompanyCodeInfo companyCodeInfo=companyCodeInfoService.getInfoByStockName(code);
        //
        List<String> days = Lists.newArrayList();
        List<BigDecimal> listOrders = thsReDianStockService.getQueryDataHistory(days,code,type);

        /**
         * 返回
         */
        String newCode="";
        if (code.startsWith("3") || code.startsWith("0")) {
            newCode = "sz" + code;
        } else {
            newCode = "sh" + code;
        }
        modelAndView.addObject("companyCodeInfo",companyCodeInfo);
        modelAndView.addObject("days",days);
        modelAndView.addObject("vols",listOrders);
        modelAndView.addObject("code",newCode);
        modelAndView.setViewName("line-simple-kandian");
        return modelAndView;
    }


    /**
     *
     */
    @RequestMapping(value = "/reDianHotRank")
    public ModelAndView reDianHotRank() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        if (null != redisUtil.get(ThsReDianStockServiceImpl.key)) {
            modelAndView.setViewName("reDianHotRank");
            return modelAndView;
        }
        List<DfcfBanKuaiMaoLiLv> result = thsReDianStockService.getReDianHotRank(Boolean.TRUE);
        //
        modelAndView.addObject("todayChengJiao", result);
        modelAndView.setViewName("reDianHotRank");
        return modelAndView;
    }
    /**
     *
     */
    @RequestMapping(value = "/select24HourData")
    public ModelAndView select24HourData() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        if (null != redisUtil.get(ThsReDianStockServiceImpl.key_24Hours)) {
            modelAndView.setViewName("reDianHotRank");
            return modelAndView;
        }
        List<DfcfBanKuaiMaoLiLv> result = thsReDianStockService.select24HourData(Boolean.TRUE);

        modelAndView.addObject("todayChengJiao", result);
        modelAndView.setViewName("reDianHotRank");
        return modelAndView;
    }





}
